package com.salinity.kun.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;

/**
 * 尝试高低潮插值
 * 
 * @author Administrator
 *
 */
public class HighLowTideInterpolationTask implements ITask {

	public List<StationDateData> readTideFile(Path filePath) throws IOException {
		try (BufferedReader br = Files.newBufferedReader(filePath)) {
			// 获取起始时间
			String line = "";
			String[] strArray = null;
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
			line = br.readLine();
			strArray = line.split("\\s+");
			LocalDateTime LDTime = LocalDateTime.of(Integer.parseInt(strArray[0]), Integer.parseInt(strArray[1]),
					Integer.parseInt(strArray[2]), 0, 0);

			// 提取数据
			double tempTime = 0.0;
			int lastHour = 0, tempHour = 0, tempMinute = 0;
			List<StationDateData> dataList = new ArrayList<>();
			while ((line = br.readLine()) != null) {
				strArray = line.split("\\s+");
				tempTime = Double.parseDouble(strArray[0]);
				tempHour = (int) tempTime;
				tempMinute = (int) ((tempTime - tempHour) * 100);
				if (lastHour > tempHour) {
					// 第二天
					LDTime = LDTime.plusDays(1);
				}
				dataList.add(new StationDateData(null, LDTime.plusHours(tempHour).plusMinutes(tempMinute).format(df),
						Double.parseDouble(strArray[1])));
				lastHour = tempHour;
			}

			return dataList;

		}
	}

	@Override
	public boolean run() throws IOException, ParseException {

		Path inputFile = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\data\\jy2005.txt");
		Path savePath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\testify\\data\\jy2005.csv");
		List<StationDateData> dataList = readTideFile(inputFile);

		List<StationDateData> dataList1 =BaseHydrologyDataHelper.highLowTideInterpolation_V2(dataList,5);
		BaseHydrologyDataHelper.writeToFile(dataList1, savePath);
		return false;
	}

	public static void main(String[] args) {

		ITask task = new HighLowTideInterpolationTask();
		try {
			task.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
